#!/usr/bin/perl -w
use strict;

use Device::SerialPort; 

my %cmdmap = (
	'SR15' => 0xbf,
	'SR14' => 0xbe,
	'SR13' => 0xbd,
	'SR12' => 0xbc,
	'SR11' => 0xbb,
	'SR10' => 0xba,
	'SR9' => 0xb9,
	'SR8' => 0xb8,
	'SR7' => 0xb7,
	'SR6' => 0xb6,
	'SR5' => 0xb5,
	'SR4' => 0xb4,
	'SR3' => 0xb3,
	'SR2' => 0xb2,
	'SR1' => 0xb1,
	'SR0' => 0xb0,
	'LOAD' => 0x7f,
	'CLEAR' => 0x7e,
	'STOP' => 0x7d,
	'START' => 0x7c,
	'MODE' => 0x7b
);

my $command = shift || die "require a command";
my $data    = shift || 0x00;

my $outstr = "\xff\xff\xff\xaa\x01";
if (exists $cmdmap{$command}) {
	$outstr .= chr($cmdmap{$command});
} else {
	die "unknown command $command";
}

$outstr .= chr($data);

my $port = Device::SerialPort->new("/dev/ttyUSB0");

$port->baudrate(4800);
$port->parity("none");
$port->handshake("none");
$port->databits(8);
$port->stopbits(1);
$port->read_char_time(0);
$port->read_const_time(1);

# send data out
$port->write($outstr);

#warn length($outstr);
#print $outstr;

$port->close();


